package com.example.demo.use_demo.a_excel_demo;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ReflectUtil;
import com.example.demo.use_demo.a_excel_demo.entity.ValidateCodeRuleEnum;
import com.example.demo.use_demo.a_excel_demo.entity.Validator;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.*;
import java.util.function.Function;

/**
 * @author zhangqiang
 * @date 2021/6/1 20:38
 */
public class ExcelDataValidator<S> {

    private ExcelDataValidator() {
    }

    private int groupIndex = 0;
    private final Map<String, List<ValidateCodeRuleEnum>> allFieldKeyValidateCodeMap = new HashMap<>();
    /**
     * 格式验证
     */
    private final List<String> formatValidateFieldKeys = new ArrayList<>();
    private final Map<String, Integer[]> fieldKeyIntLimitMap = new HashMap<>();
    private final Map<String, BigDecimal[]> fieldKeyDecimalLimitMap = new HashMap<>();
    private final Map<String, Integer[]> fieldKeyLengthLimitMap = new HashMap<>();
    private final Map<String, Integer> fieldKeyLengthMap = new HashMap<>();
    /**
     * 范围验证
     */
    private final Map<String, Collection<?>> fieldKeyRangeDataMap = new HashMap<>();
    /**
     * 自定义验证逻辑
     */
    private static final String CUSTOM_FUNCTION_INVALID_MESSAGE_HASH_REGEX = ":invalidTip\\|";
    private final Map<String, Function<S, Boolean>> customValidateFunctionMap = new HashMap<>();
    /**
     * 不允许重复的数据-非联合
     */
    private final List<String> noRepeatFieldKeys = new ArrayList<>();
    private final Map<String, List<Object>> noRepeatFieldKeyDatasMap = new HashMap<>();
    /**
     * 不允许重复的数据-联合
     */
    private final Map<Integer, Collection<String>> groupNoRepeatFieldKeysMap = new HashMap<>();
    private final Map<Integer, List<Map<String, Object>>> groupNoRepeatFieldKeyDatasMap = new HashMap<>();
    /**
     * 验证提示信息转换
     */
    private Map<String, String> fieldKeyNameMap = new HashMap<>(16);
    private String[] fieldKeys;
    private String[] fieldNames;

    /**
     * 获取Validator
     */
    public static <S> ExcelDataValidator<S> newValidator(Class<S> beanType) {
        return new ExcelDataValidator<>();
    }

    //--------------------------------------------------wrap rule-------------------------------------------------------

    /**
     * 包装校验规则-格式
     *
     * @param validateCodeRuleEnum 规则
     * @param fieldKeys            要验证的key
     * @return this
     */
    public ExcelDataValidator<S> wrapRule(ValidateCodeRuleEnum validateCodeRuleEnum, Collection<String> fieldKeys) {
        for (String fieldKey : fieldKeys) {
            allFieldKeyValidateCodeMap.computeIfAbsent(fieldKey, k -> new ArrayList<>());
            allFieldKeyValidateCodeMap.get(fieldKey).add(validateCodeRuleEnum);
            formatValidateFieldKeys.add(fieldKey);
        }
        return this;
    }

    /**
     * 包装校验规则-格式
     *
     * @param validateCodeRuleEnum 规则
     * @param fieldKeys            要验证的key
     * @return this
     */
    public ExcelDataValidator<S> wrapRule(ValidateCodeRuleEnum validateCodeRuleEnum, String... fieldKeys) {
        return this.wrapRule(validateCodeRuleEnum, Arrays.asList(fieldKeys));
    }


    /**
     * 包装校验规则-数据大小
     *
     * @param min       最小值
     * @param max       最大值
     * @param fieldKeys 要验证的key
     * @return this
     */
    public ExcelDataValidator<S> wrapRuleWithDataIntLimit(Integer min, Integer max, Collection<String> fieldKeys) {
        for (String fieldKey : fieldKeys) {
            allFieldKeyValidateCodeMap.computeIfAbsent(fieldKey, k -> new ArrayList<>());
            allFieldKeyValidateCodeMap.get(fieldKey).add(ValidateCodeRuleEnum.NUM_RANGE);
            formatValidateFieldKeys.add(fieldKey);
            fieldKeyIntLimitMap.put(fieldKey, new Integer[]{min, max});
        }
        return this;
    }

    /**
     * 包装校验规则-数据大小
     *
     * @param min       最小值
     * @param max       最大值
     * @param fieldKeys 要验证的key
     * @return this
     */
    public ExcelDataValidator<S> wrapRuleWithDataIntLimit(Integer min, Integer max, String... fieldKeys) {
        return this.wrapRuleWithDataIntLimit(min, max, Arrays.asList(fieldKeys));
    }

    /**
     * 包装校验规则-数据大小
     *
     * @param min       最小值
     * @param max       最大值
     * @param fieldKeys 要验证的key
     * @return this
     */
    public ExcelDataValidator<S> wrapRuleWithDataDecimalLimit(BigDecimal min, BigDecimal max, Collection<String> fieldKeys) {
        for (String fieldKey : fieldKeys) {
            allFieldKeyValidateCodeMap.computeIfAbsent(fieldKey, k -> new ArrayList<>());
            allFieldKeyValidateCodeMap.get(fieldKey).add(ValidateCodeRuleEnum.DECIMAL_RANGE);
            formatValidateFieldKeys.add(fieldKey);
            fieldKeyDecimalLimitMap.put(fieldKey, new BigDecimal[]{min, max});
        }
        return this;
    }

    /**
     * 包装校验规则-数据大小
     *
     * @param min       最小值
     * @param max       最大值
     * @param fieldKeys 要验证的key
     * @return this
     */
    public ExcelDataValidator<S> wrapRuleWithDataDecimalLimit(BigDecimal min, BigDecimal max, String... fieldKeys) {
        return this.wrapRuleWithDataDecimalLimit(min, max, Arrays.asList(fieldKeys));
    }

    /**
     * 包装校验规则-数据长度
     *
     * @param length    长度
     * @param fieldKeys 要验证的key
     * @return this
     */
    public ExcelDataValidator<S> wrapRuleWithDataLength(Integer length, Collection<String> fieldKeys) {
        for (String fieldKey : fieldKeys) {
            allFieldKeyValidateCodeMap.computeIfAbsent(fieldKey, k -> new ArrayList<>());
            allFieldKeyValidateCodeMap.get(fieldKey).add(ValidateCodeRuleEnum.LENGTH);
            formatValidateFieldKeys.add(fieldKey);
            fieldKeyLengthMap.put(fieldKey, length);
        }
        return this;
    }

    /**
     * 包装校验规则-数据长度
     *
     * @param length    长度
     * @param fieldKeys 要验证的key
     * @return this
     */
    public ExcelDataValidator<S> wrapRuleWithDataLength(Integer length, String... fieldKeys) {
        return wrapRuleWithDataLength(length, Arrays.asList(fieldKeys));
    }

    /**
     * 包装校验规则-数据长度范围
     *
     * @param min       最小值
     * @param max       最大值
     * @param fieldKeys 要验证的key
     * @return this
     */
    public ExcelDataValidator<S> wrapRuleWithDataLengthLimit(Integer min, Integer max, Collection<String> fieldKeys) {
        for (String fieldKey : fieldKeys) {
            allFieldKeyValidateCodeMap.computeIfAbsent(fieldKey, k -> new ArrayList<>());
            allFieldKeyValidateCodeMap.get(fieldKey).add(ValidateCodeRuleEnum.LENGTH_LIMIT);
            formatValidateFieldKeys.add(fieldKey);
            fieldKeyLengthLimitMap.put(fieldKey, new Integer[]{min, max});
        }
        return this;
    }

    /**
     * 包装校验规则-数据长度范围
     *
     * @param min       最小值
     * @param max       最大值
     * @param fieldKeys 要验证的key
     * @return this
     */
    public ExcelDataValidator<S> wrapRuleWithDataLengthLimit(Integer min, Integer max, String... fieldKeys) {
        return wrapRuleWithDataLengthLimit(min, max, Arrays.asList(fieldKeys));
    }

    /**
     * 保证校验规则-重复
     *
     * @param isUnion   是否联合唯一
     * @param fieldKeys 要验证的key
     * @return this
     */
    public ExcelDataValidator<S> wrapRuleWithNoRepeat(boolean isUnion, Collection<String> fieldKeys) {
        if (isUnion) {
            allFieldKeyValidateCodeMap.computeIfAbsent(fieldKeys.toString(), k -> new ArrayList<>());
            allFieldKeyValidateCodeMap.get(fieldKeys.toString()).add(ValidateCodeRuleEnum.UNION_NOT_REPEATS);
            groupNoRepeatFieldKeysMap.put(groupIndex, fieldKeys);
            groupNoRepeatFieldKeyDatasMap.put(groupIndex, new ArrayList<>());
            groupIndex++;
        } else {
            for (String fieldKey : fieldKeys) {
                allFieldKeyValidateCodeMap.computeIfAbsent(fieldKey, k -> new ArrayList<>());
                allFieldKeyValidateCodeMap.get(fieldKey).add(ValidateCodeRuleEnum.NOT_REPEAT);
                noRepeatFieldKeys.add(fieldKey);
                noRepeatFieldKeyDatasMap.put(fieldKey, new ArrayList<>());
            }
        }
        return this;
    }

    /**
     * 包装校验规则-重复
     *
     * @param isUnion   是否联合唯一
     * @param fieldKeys 要验证的key
     * @return this
     */
    public ExcelDataValidator<S> wrapRuleWithNoRepeat(boolean isUnion, String... fieldKeys) {
        return this.wrapRuleWithNoRepeat(isUnion, Arrays.asList(fieldKeys));
    }

    /**
     * 包装校验规则-数据范围
     *
     * @param ruleData  数据源
     * @param fieldKeys 要验证的key
     * @return this
     */
    public ExcelDataValidator<S> wrapRuleWithDataRange(Collection<?> ruleData, Collection<String> fieldKeys) {
        for (String fieldKey : fieldKeys) {
            allFieldKeyValidateCodeMap.computeIfAbsent(fieldKey, k -> new ArrayList<>());
            allFieldKeyValidateCodeMap.get(fieldKey).add(ValidateCodeRuleEnum.CUSTOM_DATA_RANGE);
            fieldKeyRangeDataMap.put(fieldKey, ruleData);
        }
        return this;
    }

    /**
     * 包装校验规则-数据范围
     *
     * @param ruleData  数据源
     * @param fieldKeys 要验证的key
     * @return this
     */
    public ExcelDataValidator<S> wrapRuleWithDataRange(Collection<?> ruleData, String... fieldKeys) {
        return this.wrapRuleWithDataRange(ruleData, Arrays.asList(fieldKeys));
    }

    /**
     * 自定义校验规则
     *
     * @param function   自定义校验逻辑
     * @param invalidMsg 提示信息
     * @return this
     */
    public ExcelDataValidator<S> wrapRuleWithCustomFunction(Function<S, Boolean> function, String invalidMsg) {
        customValidateFunctionMap.put(function.hashCode() + ":invalidTip|" + invalidMsg, function);
        return this;
    }

    /**
     * 用于提示信息转换
     *
     * @param fieldKeyNameMap 字段名称-显示信息
     * @return this
     */
    public ExcelDataValidator<S> wrapRuleWithFieldKeyNameMap(Map<String, String> fieldKeyNameMap) {
        this.fieldKeyNameMap = fieldKeyNameMap;
        this.fieldKeys = fieldKeyNameMap.keySet().toArray(new String[]{});
        this.fieldNames = fieldKeyNameMap.values().toArray(new String[]{});
        return this;
    }

    //----------------------------------------------------validate------------------------------------------------------

    /**
     * 单个数据验证-格式
     *
     * @param fieldKey   key
     * @param fieldValue 值
     * @return 提示信息
     */
    public String validateStrFormat(String fieldKey, Object fieldValue) {
        if (formatValidateFieldKeys.contains(fieldKey)) {
            for (ValidateCodeRuleEnum validateCodeRuleEnum : allFieldKeyValidateCodeMap.get(fieldKey)) {
                switch (validateCodeRuleEnum) {
                    case DATE_YYYY:
                        if (fieldValue != null && !(fieldValue instanceof Date) && !Validator.validateDate(fieldValue.toString(), "yyyy")) {
                            return "必须是yyyy格式日期!";
                        }
                        break;
                    case DATE_YYYY_MM:
                        if (fieldValue != null && !(fieldValue instanceof Date) && !Validator.validateDate(fieldValue.toString(), "yyyy-MM")) {
                            return "必须是yyyy-MM格式日期!";
                        }
                        break;
                    case DATE_YYYY_MM_DD:
                        if (fieldValue != null && !(fieldValue instanceof Date) && !Validator.validateDate(fieldValue.toString(), "yyyy-MM-dd")) {
                            return "必须是yyyy-MM-dd格式日期!";
                        }
                        break;
                    case DATE_YYYY_MM_DD_HH_SS_MM:
                        if (fieldValue != null && !(fieldValue instanceof Date) && !Validator.validateDate(fieldValue.toString(), "yyyy-MM-dd HH:ss:mm")) {
                            return "必须是yyyy-MM格式日期!";
                        }
                        break;
                    case NOT_BLANK:
                        if (fieldValue == null || StringUtils.isBlank(fieldValue.toString())) {
                            return "不能为空";
                        }
                        break;
                    case NUM:
                        if (fieldValue != null && !(fieldValue instanceof Integer) && !Validator.validateInt(fieldValue.toString())) {
                            return "非整数类型";
                        }
                        break;
                    case NUM_RANGE:
                        if (fieldValue != null) {
                            if (!(fieldValue instanceof Integer) && !Validator.validateInt(fieldValue.toString())) {
                                return "非整数类型";
                            } else if (fieldKeyIntLimitMap.get(fieldKey)[0].compareTo(Integer.parseInt(fieldValue.toString())) > 0
                                    || fieldKeyIntLimitMap.get(fieldKey)[1].compareTo(Integer.parseInt(fieldValue.toString())) < 0) {
                                return "超出范围" + fieldKeyIntLimitMap.get(fieldKey)[0] + "-" + fieldKeyIntLimitMap.get(fieldKey)[1];
                            }
                        }
                        break;
                    case DECIMAL:
                        if (fieldValue != null && !(fieldValue instanceof BigDecimal) && !Validator.validateBigDecimal(fieldValue.toString())) {
                            return "非数值类型";
                        }
                        break;
                    case DECIMAL_RANGE:
                        if (fieldValue != null) {
                            if (!(fieldValue instanceof BigDecimal) && !Validator.validateBigDecimal(fieldValue.toString())) {
                                return "非数值类型";
                            } else if (fieldKeyDecimalLimitMap.get(fieldKey)[0].compareTo(new BigDecimal(fieldValue.toString())) > 0
                                    || fieldKeyDecimalLimitMap.get(fieldKey)[1].compareTo(new BigDecimal(fieldValue.toString())) < 0) {
                                return "超出范围" + fieldKeyDecimalLimitMap.get(fieldKey)[0] + "-" + fieldKeyDecimalLimitMap.get(fieldKey)[1];
                            }
                        }
                        break;
                    case LENGTH:
                        if (fieldValue != null) {
                            if (fieldKeyLengthMap.get(fieldKey) != fieldValue.toString().length()) {
                                return "长度限制" + fieldKeyLengthMap.get(fieldKey);
                            }
                        }
                        break;
                    case LENGTH_LIMIT:
                        if (fieldValue != null) {
                            if (fieldKeyLengthLimitMap.get(fieldKey)[0].compareTo(fieldValue.toString().length()) > 0
                                    || fieldKeyLengthLimitMap.get(fieldKey)[1].compareTo(fieldValue.toString().length()) < 0) {
                                return "长度限制" + fieldKeyLengthLimitMap.get(fieldKey)[0] + "-" + fieldKeyLengthLimitMap.get(fieldKey)[1];
                            }
                        }
                        break;
                    default:
                        break;
                }
            }
        }
        return null;
    }

    /**
     * 单个数据验证-范围
     *
     * @param fieldKey   key
     * @param fieldValue 值
     * @param <V>        值类型
     * @return 提示信息
     */
    public <V> boolean validateRange(String fieldKey, V fieldValue) {
        if (fieldKeyRangeDataMap.containsKey(fieldKey)) {
            return fieldKeyRangeDataMap.get(fieldKey).contains(fieldValue);
        }
        return true;
    }

    /**
     * 单条数据验证-自定义
     *
     * @param data 数据
     * @return 提示信息
     */
    public String validateCustomFunction(S data) {
        for (Map.Entry<String, Function<S, Boolean>> entry : customValidateFunctionMap.entrySet()) {
            if (!entry.getValue().apply(data)) {
                return entry.getKey().split(CUSTOM_FUNCTION_INVALID_MESSAGE_HASH_REGEX)[1];
            }
        }
        return null;
    }

    /**
     * 单个数据验证-重复
     *
     * @param fieldKey   key
     * @param fieldValue 值
     * @return 提示信息
     */
    public <V> boolean validateRepeat(String fieldKey, V fieldValue) {
        if (noRepeatFieldKeys.contains(fieldKey)) {
            if (noRepeatFieldKeyDatasMap.get(fieldKey).contains(fieldValue)) {
                return true;
            } else {
                noRepeatFieldKeyDatasMap.get(fieldKey).add(fieldValue);
            }
        }
        return false;
    }

    /**
     * 单条数据验证-重复
     *
     * @param dataMap 数据
     * @return 提示
     */
    @SuppressWarnings("unchecked")
    public <K extends String, V> String validateUnionRepeat(Map<K, V> dataMap) {
        Integer index;
        Collection<String> fields;
        Map<String, Object> mapTmp = null;
        boolean isContains = false;
        for (Map.Entry<Integer, Collection<String>> entry : groupNoRepeatFieldKeysMap.entrySet()) {
            index = entry.getKey();
            fields = entry.getValue();
            if (!groupNoRepeatFieldKeyDatasMap.containsKey(index)) {
                if (dataMap != null) {
                    mapTmp = new HashMap<>(2);
                    for (String field : fields) {
                        mapTmp.put(field, dataMap.get((K) field));
                    }
                    groupNoRepeatFieldKeyDatasMap.put(index, new ArrayList<>());
                    groupNoRepeatFieldKeyDatasMap.get(index).add(mapTmp);
                }
            } else {
                if (dataMap != null) {
                    mapTmp = new HashMap<>(2);
                    for (String field : fields) {
                        mapTmp.put(field, dataMap.get((K) field));
                    }
                }
                isContains = groupNoRepeatFieldKeyDatasMap.get(index).contains(mapTmp);
                if (isContains) {
                    return fields.toString();
                } else {
                    groupNoRepeatFieldKeyDatasMap.get(index).add(mapTmp);
                    return null;
                }
            }
        }
        return null;
    }

    /**
     * 单条数据验证-重复
     *
     * @param data 数据
     * @return 提示
     */
    public String validateUnionRepeat(S data) {
        Integer index;
        Collection<String> fields;
        Map<String, Object> mapTmp = null;
        boolean isContains = false;
        for (Map.Entry<Integer, Collection<String>> entry : groupNoRepeatFieldKeysMap.entrySet()) {
            index = entry.getKey();
            fields = entry.getValue();
            if (!groupNoRepeatFieldKeyDatasMap.containsKey(index)) {
                if (data != null) {
                    mapTmp = new HashMap<>(2);
                    for (String field : fields) {
                        mapTmp.put(field, ReflectUtil.getFieldValue(data, field));
                    }
                    groupNoRepeatFieldKeyDatasMap.put(index, new ArrayList<>());
                    groupNoRepeatFieldKeyDatasMap.get(index).add(mapTmp);
                }
            } else {
                if (data != null) {
                    mapTmp = new HashMap<>(2);
                    for (String field : fields) {
                        mapTmp.put(field, ReflectUtil.getFieldValue(data, field));
                    }
                }
                isContains = groupNoRepeatFieldKeyDatasMap.get(index).contains(mapTmp);
                if (isContains) {
                    return fields.toString();
                } else {
                    groupNoRepeatFieldKeyDatasMap.get(index).add(mapTmp);
                    return null;
                }
            }
        }
        return null;
    }

    //------------------------------------------------------------------------------------------------------------------

    /**
     * 单个数据验证
     *
     * @param fieldKey   key
     * @param fieldValue 值
     * @param <V>        值类型
     * @return 提示
     */
    public <K extends String, V> String validateField(K fieldKey, V fieldValue) {
        if (!this.hasValidate() || !allFieldKeyValidateCodeMap.containsKey(fieldKey)) {
            return null;
        }
        String invalidMsg = this.validateStrFormat(fieldKey, fieldValue);
        if (invalidMsg != null) {
            return this.convertFieldKeyToName(fieldKey) + invalidMsg;
        }
        boolean inRange = this.validateRange(fieldKey, fieldValue);
        if (!inRange) {
            return this.convertFieldKeyToName(fieldKey) + "数据不存在";
        }
        boolean repeat = this.validateRepeat(fieldKey, fieldValue);
        if (repeat) {
            return this.convertFieldKeyToName(fieldKey) + "数据重复";
        }
        return null;
    }

    /**
     * 单条数据验证
     *
     * @param dataMap 数据
     * @return 提示
     */
    @SuppressWarnings(value = "unchecked")
    public <V> String validateFields(Map<String, V> dataMap) {
        if (!this.hasValidate()) {
            return null;
        }
        // 单列验证-格式
        if (this.hasFormatValidate()) {
            for (String formatFieldKey : formatValidateFieldKeys) {
                String errorMsg = this.validateStrFormat(formatFieldKey, dataMap.get(formatFieldKey));
                if (errorMsg != null) {
                    return this.convertFieldKeyToName(formatFieldKey) + errorMsg;
                }
            }
        }
        // 单列验证-数据范围
        if (this.hasRangeValidate()) {
            for (String rangeFieldKey : fieldKeyRangeDataMap.keySet()) {
                boolean inRange = this.validateRange(rangeFieldKey, dataMap.get(rangeFieldKey));
                if (!inRange) {
                    return this.convertFieldKeyToName(rangeFieldKey) + "数据不存在";
                }
            }
        }
        // 单列验证-重复
        if (this.hasRepeatValidate()) {
            for (String noRepeatFieldKey : noRepeatFieldKeys) {
                boolean repeat = this.validateRepeat(noRepeatFieldKey, dataMap.get(noRepeatFieldKey));
                if (repeat) {
                    return this.convertFieldKeyToName(noRepeatFieldKey) + "数据重复";
                }
            }
        }
        // 多列验证-重复
        if (this.hasUnionRepeatValidate()) {
            String repeatFields = this.validateUnionRepeat(dataMap);
            if (repeatFields != null) {
                return this.convertFieldKeyToName(repeatFields) + "数据重复";
            }
        }
        // 多列验证-自定义
        if (this.hasCustomFunctionValidate()) {
            return this.validateCustomFunction((S) dataMap);
        }
        return null;
    }

    /**
     * 单条数据验证
     *
     * @param data 数据
     * @return 提示
     */
    public String validateFields(S data) {
        if (!this.hasValidate()) {
            return null;
        }
        Object valueTmp = null;
        // 单列验证-格式
        if (this.hasFormatValidate()) {
            for (String formatFieldKey : formatValidateFieldKeys) {
                valueTmp = ReflectUtil.getFieldValue(data, formatFieldKey);
                String errorMsg = this.validateStrFormat(formatFieldKey, valueTmp);
                if (errorMsg != null) {
                    return this.convertFieldKeyToName(formatFieldKey) + errorMsg;
                }
            }
        }
        // 单列验证-数据范围
        if (this.hasRangeValidate()) {
            for (String rangeFieldKey : fieldKeyRangeDataMap.keySet()) {
                valueTmp = ReflectUtil.getFieldValue(data, rangeFieldKey);
                boolean inRange = this.validateRange(rangeFieldKey, valueTmp);
                if (!inRange) {
                    return this.convertFieldKeyToName(rangeFieldKey) + "数据不存在";
                }
            }
        }
        // 单列验证-重复
        if (this.hasRepeatValidate()) {
            for (String noRepeatFieldKey : noRepeatFieldKeys) {
                valueTmp = ReflectUtil.getFieldValue(data, noRepeatFieldKey);
                boolean repeat = this.validateRepeat(noRepeatFieldKey, valueTmp);
                if (repeat) {
                    return this.convertFieldKeyToName(noRepeatFieldKey) + "数据重复";
                }
            }
        }
        // 多列验证-重复
        if (this.hasUnionRepeatValidate()) {
            String repeatFields = this.validateUnionRepeat(data);
            if (repeatFields != null) {
                return this.convertFieldKeyToName(repeatFields) + "数据重复";
            }
        }
        // 多列验证-自定义
        if (this.hasCustomFunctionValidate()) {
            return this.validateCustomFunction(data);
        }
        return null;
    }

    /**
     * 是否有需要验证的field
     */
    public boolean hasValidate() {
        return CollectionUtil.isNotEmpty(allFieldKeyValidateCodeMap) || this.hasCustomFunctionValidate();
    }

    /**
     * 是否有格式验证field
     */
    public boolean hasFormatValidate() {
        return CollectionUtil.isNotEmpty(formatValidateFieldKeys);
    }

    /**
     * 是否有重复验证field
     */
    public boolean hasRepeatValidate() {
        return CollectionUtil.isNotEmpty(noRepeatFieldKeys);
    }

    /**
     * 是否有联合重复验证field
     */
    public boolean hasUnionRepeatValidate() {
        return CollectionUtil.isNotEmpty(groupNoRepeatFieldKeysMap);
    }

    /**
     * 是否有数据范围验证field
     */
    public boolean hasRangeValidate() {
        return CollectionUtil.isNotEmpty(fieldKeyRangeDataMap);
    }

    /**
     * 是否有自定义验证逻辑function
     */
    public boolean hasCustomFunctionValidate() {
        return CollectionUtil.isNotEmpty(customValidateFunctionMap);
    }

    /**
     * 转换fieldKey
     */
    private String convertFieldKeyToName(String fieldKey) {
        if (CollectionUtil.isEmpty(fieldKeyNameMap)) {
            return fieldKey;
        }
        return StringUtils.replaceEach(fieldKey, fieldKeys, fieldNames);
    }
}
